package com.example.dllo.exe.fenlei.danpin;

import android.content.Context;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.example.dllo.exe.R;
import com.example.dllo.exe.bean.DanPinRightBean;
import com.example.dllo.exe.fengzhuang.DBValue;


/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class DanPinRightAdapter extends BaseAdapter {
    private Context context;
    private DanPinRightBean danPinRightBean;


    public void setDanPinRightBean(DanPinRightBean danPinRightBean) {
        this.danPinRightBean = danPinRightBean;
    }

    public DanPinRightAdapter(Context context) {
        this.context = context;

    }


    @Override
    public int getCount() {
        return danPinRightBean != null ? danPinRightBean.getData().getCategories().size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(context);
        if (view == null) {
            view = inflater.inflate(R.layout.danpinrightrvlayout, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        GridLayoutManager manager = new GridLayoutManager(view.getContext(), 3);
        viewHolder.recycleviewrightdanpin.setLayoutManager(manager);
        DanpinRVRightAdapter danpinRVRightAdapter = new DanpinRVRightAdapter(view.getContext());
        danpinRVRightAdapter.setDanPinRightBean(danPinRightBean);
        danpinRVRightAdapter.setI(i);
        viewHolder.recycleviewrightdanpin.setAdapter(danpinRVRightAdapter);
        viewHolder.textviewdanpinfenleibiaoti.setVisibility(View.VISIBLE);
        viewHolder.textviewdanpinfenleibiaoti.setText(danPinRightBean.getData().getCategories().get(i).getName());



        danpinRVRightAdapter.setItemonClick(new DanpinRVRightAdapter.Itemclick() {
            @Override
            public void Itemclick(int pos, String name) {
                //拼接之后的网址

                int url = danPinRightBean.getData().getCategories().get(i).getSubcategories().get(pos).getId();
                String urlpinjie = DBValue.FENLEI_DANPINQIANZHUI + url + DBValue.FENLEI_DANPINHOUZHUI;


                Intent intent = new Intent(context, FenLeiDanPinJieAty.class);
                intent.putExtra("key", urlpinjie);
                intent.putExtra("name", name);
                context.startActivity(intent);

            }
        });
        return view;
    }

    public class ViewHolder {
        TextView textviewdanpinfenleibiaoti;
        RecyclerView recycleviewrightdanpin;

        public ViewHolder(View view) {

            recycleviewrightdanpin = (RecyclerView) view.findViewById(R.id.recycleviewright_danpin);
            textviewdanpinfenleibiaoti = (TextView) view.findViewById(R.id.text_danpinbiaoti);

        }
    }

}
